<template>
  <!-- 每月一学、每月一案 -->
  <base-breadcrumb :title="$route.meta.title">
    <!-- 搜索 -->
    <searchData
      :search-data="searchArr"
      @search="search"
    />
    <!-- 表格 -->
    <a-card :bordered="false">
      <!-- <title-name :title="$route.meta.title" /> -->
      <a-button
        v-btnPermission="'di_005_add_01'"
        class="mb12"
        icon="plus"
        type="primary"
        @click="handleManage(2)"
      >
        新增学习任务
      </a-button>
      <!-- 表格 -->
      <list-table
        ref="table"
        table-layout="fixed"
        size="default"
        row-key="id"
        :columns="columns"
        :request="loadData"
        :alert="true"
        show-size-changer
        show-quick-jumper
        show-pagination="auto"
      >
        <template
          slot="feedbackTime"
          slot-scope="text, record"
        >
          {{ record.feedbackVO ? record.feedbackVO.feedbackTime : '—' }}
        </template>
        <template
          slot="enabled"
          slot-scope="text, record"
        >
          {{ record.statusName }}
        </template>
        <template
          slot="action"
          slot-scope="text, record"
        >
          <!--0 未发送;1 已撤销; 2 已发送-->
          <!--未发送 && 已撤销：发送/查看/编辑/删除-->
          <!--已发送：查看、撤销-->
          <MoreButton>
          <span
            v-if="record.status != 2 && record.creatorId === creatorId"
            v-btnPermission="'di_005_sot_04'"
          >
            <a @click="handleSend(record,1)">发送</a>
          </span>
          <span v-btnPermission="'di_005_loo_04'">
            <a @click="handleManage(0, record)">查看</a>
          </span>
          <span
            v-if="record.status != 2 && record.creatorId === creatorId"
            v-btnPermission="'di_005_edi_02'"
          >
            <a @click="handleManage(1, record)">编辑</a>
          </span>
          <span
            v-if="record.status != 2 && record.creatorId === creatorId"
            v-btnPermission="'di_005_del_03'"
          >
            <a
              class="del_btn"
              @click="handleRemove(record)"
            >删除</a>
          </span>
          <a
            v-if="record.status == 2 && record.creatorId === creatorId"
            v-btnPermission="'di_005_cep_04'"
            class="del_btn"
            @click="handleSend(record,2)"
          >撤销</a>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
  import { STable } from '@/components' // 表格
  import { getList, remove, send } from '@/api/disciplineInspection/culturalEducation/month' // api
  import BasePage from '@/views/basePage' // 基础配置

  const searchArr = [
    // 搜索框
    {
      name: '学习名称',
      type: 'input',
      value: 'title',
    },
    {
      name: '月份',
      type: 'month',
      value: 'monthTime',
    },
  ]
  const columns = [
    {
      title: '序号',
      width: 80,
      dataIndex: 'index',
      customRender() {
        return arguments[2] + 1
      },
    },
    {
      title: '月份',
      ellipsis: true,
      type: 'a-month-picker',
      dataIndex: 'monthTime',
    },
    {
      title: '学习名称',
      ellipsis: true,
      dataIndex: 'title',
    },
    {
      title: '学习内容',
      ellipsis: true,
      dataIndex: 'content',
    },
    {
      title: '发布者',
      ellipsis: true,
      dataIndex: 'creator',
    },
    {
      title: '发布时间',
      ellipsis: true,
      dataIndex: 'createTime',
    },
    {
      title: '发送状态',
      dataIndex: 'statusName',
      scopedSlots: {
        customRender: 'enabled',
      },
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      width: '180px',
      align: 'center',
      scopedSlots: { customRender: 'action' },
    },
  ]
  export default {
    name:'a'+ Date.now(),
    extends: new BasePage(),
    components: {
      STable,
    },
    data() {
      this.columns = columns
      this.searchArr = searchArr
      return {
        creatorId : JSON.parse(sessionStorage.getItem('ycloud-user_info')).id,
        queryParam: {}, // 查询参数
        loadData: (parameter) => {
          const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
          return getList(requestParameters).then((res) => {
            return res
          })
        },// 加载数据方法 必须为 Promise 对象
        removeApi: { remove: remove }, // api
        editModel: 0,
        form: {},
      }
    },
    methods: {
      // 搜索
      search(value) {
        value && (this.queryParam = value)
        this.$refs.table.refresh(true)
      },
      /**
       * 发送/撤销
       **/
      handleSend(e,index){
        this.$confirm({
          title: '温馨提示',
          content: index == 1 ? `确认发送${e.title}？` : `确认撤销${e.title}？`,
          onOk: () => {
            send(e.id)
              .then(() => {
                const msg = index == 1 ? '发送成功！' : '操作成功'
                this.$message.success(msg)
                this.search()
              })
              .catch(() => {
                that.loading = false
              })
          },
        })
      },
      /**
       * 删除
       **/
      handleRemove(e){
        this.$confirm({
          title: '温馨提示',
          content: `确认移除${e.title}？`,
          onOk: () => {
            return remove(e.id).then(() => {
              this.$message.success('移除成功！')
              this.search()
            })
          },
        })
      },
      // 操作栏
      handleManage(type, record) {
        this.editModel = type
        let query = {
          editModel: type,
        }
        if (record && record.id) {
          query.id = record.id
        }
        this.$router.push({
          path: '/discipline/culturalEducation/month/add',
          query,
        })
      },
    },
  }
</script>
